package com.ddtech.business.sys.rest;

import com.ddtech.business.sys.query.SysToolQuery;
import com.ddtech.business.sys.service.SysToolService;
import com.ddtech.framework.log.aop.log.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 系统工具前端控制器
 * </p>
 *
 * @author bin_pu
 * @since 2020-04-14
 */
@Api(tags = "业务-系统工具")
@RestController
@RequestMapping("/api")
public class SysToolController {

    @Autowired
    private SysToolService sysToolService;

    @Log("查询系统工具文件")
    @ApiOperation(value = "查询系统工具文件")
    @GetMapping(value = "/sysTool")
    public ResponseEntity geSysToolPage(SysToolQuery sysToolQuery) {
        return new ResponseEntity(sysToolService.queryAll(sysToolQuery), HttpStatus.OK);
    }

    @Log("上传系统工具文件")
    @ApiOperation(value = "上传系统工具文件")
    @PostMapping(value = "/sysTool")
    @PreAuthorize("hasAnyRole('ADMIN','SYS_TOOL_ALL','SYS_TOOL_CREATE')")
    public ResponseEntity create(@RequestParam(required = false) String type, @RequestParam("file") MultipartFile file) {
        return new ResponseEntity(sysToolService.create(type, file), HttpStatus.CREATED);
    }

    @Log("删除系统工具文件")
    @ApiOperation(value = "删除系统工具文件")
    @DeleteMapping(value = "/sysTool/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','SYS_TOOL_ALL','SYS_TOOL_DELETE')")
    public ResponseEntity delete(@PathVariable String id) {
        sysToolService.deleteAll(id.split(","));
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("多选删除系统工具")
    @DeleteMapping(value = "/sysTool")
    @PreAuthorize("hasAnyRole('ADMIN','SYS_TOOL_ALL','SYS_TOOL_DELETE')")
    public ResponseEntity deleteAll(@RequestBody String[] ids) {
        sysToolService.deleteAll(ids);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("下载系统工具")
    @GetMapping(value = "/sysTool/downLoad/{id}")
    public ResponseEntity downLoad(HttpServletResponse response, @PathVariable String id) {
        sysToolService.downLoad(response, id);
        return new ResponseEntity(HttpStatus.OK);
    }

}
